package com.easepal802s.project.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.clj.fastble.data.BleDevice;
import com.easepal802s.project.utils.ActivityUtils;
import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.R;
import com.easepal802s.project.observer.BleDataObserver;
import com.easepal802s.project.observer.CloseObserver;
import com.easepal802s.project.ui.presenter.ProMasDetailPresenter;
import com.easepal802s.project.ui.iview.IProMasDetailView;
import com.easepal802s.project.ble.BleController;
import com.easepal802s.project.view.LongClickImageView;
import com.easepal802s.project.widget.LayoutBody;
import com.easepal802s.project.widget.LayoutFunctionView;
import com.easepal802s.project.widget.LayoutGears5;
import com.easepal802s.project.widget.LayoutGears6;
import com.easepal802s.project.widget.LayoutNormalTitle;
import com.easepal802s.project.view.MoveLayout;
import com.ogawa.base.utils.CommonUtil;

import java.lang.ref.WeakReference;

import static com.easepal802s.project.ble.BleController.CONNECT_BREAK;

/**
 * 专业按摩 按摩页面
 * Created by FHB on 2018/3/27.
 */

public class ProMasDetailActivity extends BaseMvpTestActivity<ProMasDetailPresenter, ProMasDetailActivity> implements View.OnClickListener, IProMasDetailView,
        ScanShoulderActivity.onCloseScan,ShouldersAdjustmentActivity.onCloseAdjust,MoveLayout.OnChooseAction, BleDataObserver.OnBleListerner {

    private RelativeLayout mLl3D;
    private RelativeLayout mLlStrong;
    private TextView mTvTime;
    private ImageView mIvStart;
    private LayoutNormalTitle mLayoutTitle;
    private LayoutBody mLayoutBody;
    private LayoutFunctionView mLayoutFunction;
    private String mName;
    private static final int TIME = 1;
    private static final int THREE_D = 2;//3D
    private static final int AIR = 3;//
    private MyHandler myHandler;
    private LongClickImageView mIvAdd3D;  //3D+
    private LongClickImageView mIvSub3D; //3D-
    private LongClickImageView mIvAddAir;
    private LongClickImageView mIvSubAir;
    private LayoutGears6 mLayout3D;
    private LayoutGears5 mLayoutAir;
    private String[] name = {"3D","强度"};
    private MoveLayout mLayout;
    private ScrollView mScroll;
    private boolean isFistAdjust  = true;
    private boolean isFirt=true;
    private boolean isStartTo;
    private int SkillResult=-1;
    private int AutoResult=-1;
    private int AirResult=-1;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pro_massage_detail);
        mName = getIntent().getStringExtra("name");
        ScanShoulderActivity.setScanAction(this);
        ShouldersAdjustmentActivity.setAdjustAction(this);
        BleDataObserver.getInst().registerBleDataObserver(this);
        initView();
    }

    @Override
    public ProMasDetailPresenter initPresenter() {
        return new ProMasDetailPresenter(this);
    }

    @Override
    protected void initCreateView(Bundle savedInstanceState) {


    }

    @Override
    public ProMasDetailActivity initActivity() {
        return this;
    }

    private void initView() {
        myHandler = new MyHandler(this);

        mLl3D = findViewById(R.id.layout_3d);
        mLlStrong = findViewById(R.id.linear_strong);
        mTvTime = findViewById(R.id.showTime);
        mLayoutBody = findViewById(R.id.layout_body_view);
        mLayoutFunction = findViewById(R.id.activity_pro_mas_functionview);
        mScroll =  findViewById(R.id.activity_detail_scroll);
        mLayout=  findViewById(R.id.detail_move_layout);
        mLayout.setData(name);
        mLayout.setScollView(mScroll);
        mLayout.setAction(this);

        mLayout3D = findViewById(R.id.layout_3d);
        mLayoutAir = findViewById(R.id.linear_strong);
        mIvAdd3D = findViewById(R.id.iv_gears6_add);
        mIvAdd3D.intiListener();
        mIvSub3D = findViewById(R.id.iv_gears6_sub);
        mIvSub3D.intiListener();
        mIvAddAir = findViewById(R.id.iv_gears5_add);
        mIvAddAir.intiListener();
        mIvSubAir = findViewById(R.id.iv_gears5_sub);
        mIvSubAir.intiListener();
        mIvStart = findViewById(R.id.imgSwitch);
        mIvAdd3D.setOnClickListener(this);
        mIvSub3D.setOnClickListener(this);
        mIvAddAir.setOnClickListener(this);
        mIvSubAir.setOnClickListener(this);


        mTvTime.setOnClickListener(this);
        mIvStart.setOnClickListener(this);

        mLayoutTitle = findViewById(R.id.activity_pro_detail_title);
        mLayoutTitle.setGreenBackBotton();
        mLayoutTitle.setTitle(mName+"");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLayoutBody.onDestory();
        mLayoutTitle.onDestory();
        mLayoutFunction.onDestory();
        BleDataObserver.getInst().removeBleDataObserver(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imgSwitch:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.PAUSE, true);
                }
                break;
            case R.id.showTime:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.TIME, true);
                }

                break;
            case R.id.iv_gears6_add:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.JIX3DADD, true);
                }
                break;
            case R.id.iv_gears6_sub:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.JIX3DSUB, true);
                }
                break;
            case R.id.iv_gears5_add:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.CONGQIQDADD, true);
                }
                break;
            case R.id.iv_gears5_sub:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.CONGQIQDSUB, true);
                }
                break;

        }
    }


    @Override
    public void get3D(int position) {
        Log.e("ProMas3D", position + "");
        myHandler.obtainMessage(THREE_D, position).sendToTarget();
    }

    @Override
    public void getAir(int position) {
        Log.e("ProMasAir", position + "");
        myHandler.obtainMessage(AIR, position).sendToTarget();
    }

    @Override
    public void isStart(boolean isStart) {
        //暂停运行 显示运行图标 正在运行 显示暂停图标
        mIvStart.setSelected(!isStart);
    }

    @Override
    public void reTime(int min, int sec) {
        myHandler.obtainMessage(TIME, min, sec).sendToTarget();
    }

    @Override
    public void showScan() {
        if(isFistAdjust){
            ActivityUtils.startScanShoulderActivity(this);
        }

    }




    @Override
    public void scanClose() {
        isFistAdjust = false;
//        this.finish();
    }

    @Override
    public void adjustClose() {
        this.finish();
    }

    @Override
    public void onChoose(int position) {
        mLayout.setTectColor(position);
        switch (position){
            case 0:
                mLlStrong.setVisibility(View.GONE);
                mLl3D.setVisibility(View.VISIBLE);

                break;
            case 1:
                mLlStrong.setVisibility(View.VISIBLE);
                mLl3D.setVisibility(View.GONE);
                break;
        }

    }

    @Override
    public void onConnectState(int state) {

    }

    @Override
    public void onTargetDeviceFind(BleDevice bleDevice) {

    }

    @Override
    public void onStarScan() {

    }

    @Override
    public void onNoDeviceFind() {

    }

    @Override
    public void onGetData(String data) {
        mBasePresenter.getDatas(data);
    }

    @Override
    public void onPowerOff() {
        if(BleController.getInst().getConnectState()==CONNECT_BREAK){
            ActivityUtils.startHomeActivity(this,"BREAK");
        }
        else {
            ActivityUtils.startHomeActivity(this,"none");
        }
        this.finish();

    }

    @Override
    public void onDataChange(String bage, int auto, int skill, int air,String name,String commond) {
        if(!mName.equals(name)){
            mName = name;
            myHandler.obtainMessage(10).sendToTarget();
        }
           if(auto==0){
               if (!isStartTo) {
                   isStartTo = true;
                   CloseObserver.getInst().noticeonClose();

                   this.finish();
                   ActivityUtils.startAdvancedMassageActivity(this);


               }

                }
    }

    @Override
    public void onShoulderAdjust(int data) {
        if(CommonUtil.isActivityTop(this.getClass(),this)&&data==1){
            ActivityUtils.startShouldersAdjustmentActivity(this);
        }
        if(data==0){
            isFistAdjust =true;
        }
    }


    private class MyHandler extends Handler {
        WeakReference<ProMasDetailActivity> mProMasDetailActivityWeakReference;

        MyHandler(ProMasDetailActivity mProMasDetailActivity) {
            mProMasDetailActivityWeakReference = new WeakReference<>(mProMasDetailActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            final ProMasDetailActivity mHomeActivity = mProMasDetailActivityWeakReference.get();
            if (mHomeActivity != null) {
                switch (msg.what) {
                    case TIME:
                        //时间
                        String min = String.valueOf(msg.arg1);
                        String sec = String.valueOf(msg.arg2);
                        if(min.length()==1){
                            min = "0"+min;
                        }
                        if(sec.length()==1){
                            sec = "0"+sec;
                        }
                        mTvTime.setText(min + " : " + sec);
                        break;
                    case THREE_D:
                        int position3d = (int) msg.obj;
                        mLayout3D.setShow(position3d);
                        break;
                    case AIR:
                        int positionAir = (int) msg.obj;
                        mLayoutAir.setShow(positionAir);
                        break;
                    case 10:
                        mLayoutTitle.setTitle(mName);
                        break;

                }
            }
        }
    }

}
